package com.ganzi.ganziitem.netty.util;

import com.ganzi.ganziitem.netty.NettyServerListener;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class NettyUtil {

    private static Logger log = LoggerFactory.getLogger(NettyUtil.class);


    public boolean sengcommandByIp(String ip,String command){
        boolean hasIp = NettyServerListener.channelMap.containsKey(ip);
        if(hasIp){
            try{
                ChannelHandlerContext ctx = NettyServerListener.channelMap.get(ip);
                sengcommand(ctx,command);
                return false;
            }catch (Exception e){
                return false;
            }
        }else{
            return false;
        }
    }

    public boolean sengcommand(ChannelHandlerContext ctx,String command){
        byte[] sbuf = CRC16M.getSendBuf(command);
        try{
            if(ctx != null){
                ctx.writeAndFlush(sbuf);
                return true;
            }
            return false;
        }catch (Exception e){
            return false;
        }
    }
}
